package com.example.zqm.git72.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zqm.git72.JavaBean.Activity;
import com.example.zqm.git72.R;
import com.example.zqm.git72.Service.ActivityService;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ActivityDetailActivity extends AppCompatActivity {
    String id;
    private TextView district_name;
    private ImageView activitypic;
    private TextView time;
    private TextView price;
    private TextView price1;
    private TextView tel;
    private TextView intro;
    private TextView enroll;
    private TextView name;
    Retrofit retrofit;
    public Activity activity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activitydetail);
        intNN();
        initView();
        intTT();
    }
    public void initView(){
        district_name = (TextView)findViewById(R.id.district_name);
        time = (TextView)findViewById(R.id.time);
        price = (TextView)findViewById(R.id.price);
        price1 = (TextView)findViewById(R.id.price1);
        tel = (TextView)findViewById(R.id.tel);
        intro = (TextView)findViewById(R.id.intro);
        enroll = (TextView)findViewById(R.id.enroll);
        name =(TextView)findViewById(R.id.name);
        activitypic = (ImageView)findViewById(R.id.activitypic);

    }

    protected void intNN(){
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        id=bundle.getString("id");
        retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ActivityService activityService = retrofit.create(ActivityService.class);
        Call <Activity> call = activityService.getActivityById(id);
        call.enqueue(new Callback<Activity>() {
            @Override
            public void onResponse(Call<Activity> call, Response<Activity> response) {
                activity=response.body();
                name.setText(activity.getActivity_name());
                district_name.setText(activity.getPlace());
                time.setText(activity.getEnd_date());
                price.setText( activity.getActivity_price());
                price1.setText(activity.getActivity_price_stu());
                tel.setText(activity.getContact_tel());
                intro.setText(activity.getIntroduction());
                String picUrl = "http://172.24.10.175/workout/Uploads/" + activity.getThumb();
                Picasso.with(ActivityDetailActivity.this).load(picUrl.toString())
                        .resize(200,50).into(activitypic);

            }

            @Override
            public void onFailure(Call<Activity> call, Throwable t) {

            }
        });
    }

    public void intTT(){
        enroll.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(ActivityDetailActivity.this,ActivityOrderActivity.class);
               String activityname = name.getText().toString();
                String activityprice = price.getText().toString();
                String activityprice1 = price1.getText().toString();
                String activityintro = intro.getText().toString();
                intent.putExtra("activityname",activityname);
                intent.putExtra("activityprice",activityprice);
                intent.putExtra("activityprice1",activityprice1);
                intent.putExtra("activityintro",activityintro);
                intent.putExtra("id",id);
//                setResult(RESULT_OK,intent);
//                Bundle bundle = new Bundle();
//                Double numb = Double.parseDouble(activityprice);
//                Double numb1 = Double.parseDouble(activityprice1);
//                bundle.putDouble("activity_price",numb);
//                bundle.putDouble("activity_price1",numb1);
                startActivity(intent);
            }
        });

    }
}
